package cloudPayAdmin.admin.service.traffic;

import cloudPayAdmin.admin.dbapp.entity.hicatcard.card.TCard;
import cloudPayAdmin.admin.dbapp.entity.traffic.TTrafficDetector;
import cloudPayAdmin.admin.dbapp.repo.traffic.TrafficDetectorRepo;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class TrafficDetectorService {

    @Autowired
    TrafficDetectorRepo trafficDetectorRepo;
    
    @Transactional(readOnly=true)
    public List<TTrafficDetector> findAll() {
    	return trafficDetectorRepo.findAll(new Specification<TTrafficDetector>() {				
			@Override
			public Predicate toPredicate(Root<TTrafficDetector> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Path<Boolean> enable = root.get("enable");
				query.where(cb.equal(enable, Boolean.TRUE));
				return null;
			}
		});
    }
    
    @Transactional(readOnly=true)
    public TTrafficDetector findById(Long trafficDetectorId) {
    	return trafficDetectorRepo.findOne(trafficDetectorId);
    }

    public void save(TTrafficDetector trafficDetector){
        trafficDetectorRepo.save(trafficDetector);
    }

    public void deleteById(Long trafficDetectorId) {
        if(trafficDetectorId != null) {
            TTrafficDetector trafficDetector = trafficDetectorRepo.findOne(trafficDetectorId);
            if(trafficDetector != null) {
                trafficDetector.setEnable(false);
                trafficDetectorRepo.save(trafficDetector);
            }
        }
    }

}
